package com.siwei.mes.entity.erp;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.siwei.mes.entity.experiment.Experiment;
import com.siwei.mes.entity.experiment.ItemorderExtend;
import com.siwei.mes.enums.EnturstReasonEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;

import javax.persistence.Column;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.io.Serializable;
import java.util.Date;
import java.util.List;


/**
 * ERP-小票(Itemorder)实体类
 *
 * @author linzi
 * @since 2024-07-29 16:13:08
 */
@Data
@Accessors(chain = true)
@Table(name = "itemorder")
@ApiModel(value = "Itemorder", description = "ERP-小票")
public class Itemorder implements Serializable {
    private static final long serialVersionUID = -55602452807604739L;
    /**
     * 小票id
     */
    @Column(name = "ITID")
    @ApiModelProperty(value = "小票id", name = "itid")
    private Integer itid;
    /**
     * 小票编号
     */
    @Column(name = "itemOrderNo")
    @ApiModelProperty(value = "小票编号", name = "itemorderno")
    private String itemorderno;
    /**
     * 拌站id
     */
    @Column(name = "deliverCompanyId")
    @ApiModelProperty(value = "拌站id", name = "delivercompanyid")
    private Integer delivercompanyid;
    /**
     * 唯一键
     */
    @Column(name = "uniqueKey")
    @ApiModelProperty(value = "唯一键", name = "uniquekey")
    private String uniquekey;
    /**
     * 父小票id
     */
    @Column(name = "masterITID")
    @ApiModelProperty(value = "父小票id", name = "masteritid")
    private Integer masteritid;
    /**
     * 1-一车多票的主票
     */
    @Column(name = "hasSlave")
    @ApiModelProperty(value = "1-一车多票的主票", name = "hasslave")
    private Integer hasslave;
    /**
     * 任务单号
     */
    @Column(name = "FRwdh")
    @ApiModelProperty(value = "任务单号", name = "frwdh")
    private Integer frwdh;
    /**
     * 车号
     */
    @Column(name = "carNumber")
    @ApiModelProperty(value = "车号", name = "carnumber")
    private String carnumber;
    /**
     * 车辆id
     */
    @Column(name = "vehicleId")
    @ApiModelProperty(value = "车辆id", name = "vehicleid")
    private Integer vehicleid;
    /**
     * 司机姓名
     */
    @Column(name = "driverName")
    @ApiModelProperty(value = "司机姓名", name = "drivername")
    private String drivername;
    /**
     * 砼品种
     */
    @Column(name = "tpz")
    @ApiModelProperty(value = "砼品种", name = "tpz")
    private String tpz;
    /**
     * 塌落度
     */
    @Column(name = "tld")
    @ApiModelProperty(value = "塌落度", name = "tld")
    private String tld;
    /**
     * 浇筑部位
     */
    @Column(name = "jzbw")
    @ApiModelProperty(value = "浇筑部位", name = "jzbw")
    private String jzbw;
    /**
     * 浇筑方式
     */
    @Column(name = "jzfs")
    @ApiModelProperty(value = "浇筑方式", name = "jzfs")
    private String jzfs;
    /**
     * 工程名称
     */
    @Column(name = "projectName")
    @ApiModelProperty(value = "工程名称", name = "projectname")
    private String projectname;
    /**
     * 工程地址
     */
    @Column(name = "projectAddress")
    @ApiModelProperty(value = "工程地址", name = "projectaddress")
    private String projectaddress;
    /**
     * 备注
     */
    @Column(name = "remarks")
    @ApiModelProperty(value = "备注", name = "remarks")
    private String remarks;
    /**
     * 收货方名字
     */
    @Column(name = "receiveCompanyName")
    @ApiModelProperty(value = "收货方名字", name = "receivecompanyname")
    private String receivecompanyname;
    /**
     * 总发货数量
     */
    @Column(name = "fhQuantity")
    @ApiModelProperty(value = "总发货数量", name = "fhquantity")
    private Float fhquantity;
    /**
     * 混凝土生产方量
     */
    @Column(name = "scQuantity")
    @ApiModelProperty(value = "混凝土生产方量", name = "scquantity")
    private Float scquantity;
    /**
     * 砂浆生产方量
     */
    @Column(name = "sjscQuantity")
    @ApiModelProperty(value = "砂浆生产方量", name = "sjscquantity")
    private Float sjscquantity;
    /**
     * 生产总方量
     */
    @Column(name = "totalScQuantity")
    @ApiModelProperty(value = "生产总方量", name = "totalscquantity")
    private Float totalscquantity;
    /**
     * 剩余方量
     */
    @Column(name = "syQuantity")
    @ApiModelProperty(value = "剩余方量", name = "syquantity")
    private Float syquantity;
    /**
     * 签收方量
     */
    @Column(name = "qrQuantity")
    @ApiModelProperty(value = "签收方量", name = "qrquantity")
    private Float qrquantity;
    /**
     * 总盘数
     */
    @Column(name = "totalpan")
    @ApiModelProperty(value = "总盘数", name = "totalpan")
    private Integer totalpan;
    /**
     * 各盘信息 json
     */
    @Column(name = "paninfo")
    @ApiModelProperty(value = "各盘信息 json", name = "paninfo")
    private String paninfo;
    /**
     * 累计方量
     */
    @Column(name = "finishQuantity")
    @ApiModelProperty(value = "累计方量", name = "finishquantity")
    private Float finishquantity;
    /**
     * 累计车数
     */
    @Column(name = "finishCarNumber")
    @ApiModelProperty(value = "累计车数", name = "finishcarnumber")
    private Integer finishcarnumber;
    /**
     *
     */
    @Column(name = "timesinfo")
    @ApiModelProperty(value = " ", name = "timesinfo")
    private String timesinfo;
    /**
     * 拌台号
     */
    @Column(name = "mixtable")
    @ApiModelProperty(value = "拌台号", name = "mixtable")
    private String mixtable;
    @Column(name = "MID")
    @ApiModelProperty(value = "${column.comment}", name = "mid")
    private Integer mid;
    /**
     * 1-小票 2-水票
     */
    @Column(name = "itemtype")
    @ApiModelProperty(value = "1-小票 2-水票", name = "itemtype")
    private Integer itemtype;
    /**
     * 额外信息
     */
    @Column(name = "extrainfo")
    @ApiModelProperty(value = "额外信息", name = "extrainfo")
    private String extrainfo;
    /**
     * 出厂时间
     */
    @Column(name = "startTime")
    @ApiModelProperty(value = "出厂时间", name = "starttime")
    private Date starttime;
    /**
     * 到达时间
     */
    @Column(name = "arriveTime")
    @ApiModelProperty(value = "到达时间", name = "arrivetime")
    private Date arrivetime;
    /**
     * 离开时间
     */
    @Column(name = "leaveTime")
    @ApiModelProperty(value = "离开时间", name = "leavetime")
    private Date leavetime;
    /**
     * 签收时间
     */
    @Column(name = "signTime")
    @ApiModelProperty(value = "签收时间", name = "signtime")
    private Date signtime;
    /**
     * 回厂时间
     */
    @Column(name = "returnTime")
    @ApiModelProperty(value = "回厂时间", name = "returntime")
    private Date returntime;
    /**
     * 是否签收
     */
    @Column(name = "isSign")
    @ApiModelProperty(value = "是否签收", name = "issign")
    private Integer issign;
    /**
     * 签收照片
     */
    @Column(name = "signImg")
    @ApiModelProperty(value = "签收照片", name = "signimg")
    private String signimg;
    /**
     * 0-作废 1-生产中(waiting) 2-运输中(finish) 3-已完成(complete)
     */
    @Column(name = "state")
    @ApiModelProperty(value = "0-作废 1-生产中(waiting) 2-运输中(finish) 3-已完成(complete)", name = "state")
    private Integer state;
    /**
     * 1- 生产中作废 2 - 生产完成作废
     */
    @Column(name = "canceltype")
    @ApiModelProperty(value = "1- 生产中作废 2 - 生产完成作废 ", name = "canceltype")
    private Integer canceltype;
    /**
     * tgl小票号
     */
    @Column(name = "jlbIds")
    @ApiModelProperty(value = "tgl小票号", name = "jlbids")
    private String jlbids;
    /**
     * 操作员
     */
    @Column(name = "czy")
    @ApiModelProperty(value = "操作员", name = "czy")
    private String czy;
    /**
     * 运距
     */
    @Column(name = "transportDistance")
    @ApiModelProperty(value = "运距", name = "transportdistance")
    private Float transportdistance;
    /**
     * 砼行id
     */
    @Column(name = "THID")
    @ApiModelProperty(value = "砼行id", name = "thid")
    private Integer thid;
    /**
     * 创建时间
     */
    @Column(name = "createTime")
    @ApiModelProperty(value = "创建时间", name = "createtime")
    private Date createtime;
    /**
     * 修改时间
     */
    @Column(name = "updateTime")
    @ApiModelProperty(value = "修改时间", name = "updatetime")
    private Date updatetime;
    /**
     * 打印次数
     */
    @Column(name = "printcount")
    @ApiModelProperty(value = "打印次数", name = "printcount")
    private Integer printcount;
    /**
     * 最后一次剩余方量
     */
    @Column(name = "lastSyQuantity")
    @ApiModelProperty(value = "最后一次剩余方量", name = "lastsyquantity")
    private Float lastsyquantity;
    @Column(name = "lastItId")
    @ApiModelProperty(value = "${column.comment}", name = "lastitid")
    private Integer lastitid;
    @Column(name = "lastTHID")
    @ApiModelProperty(value = "${column.comment}", name = "lastthid")
    private Integer lastthid;
    /**
     * 小票生产时的容重
     */
    @Column(name = "rz")
    @ApiModelProperty(value = "小票生产时的容重", name = "rz")
    private Float rz;
    /**
     * 出厂方量
     */
    @Column(name = "ccQuantity")
    @ApiModelProperty(value = "出厂方量", name = "ccquantity")
    private Float ccquantity;
    /**
     * 回厂方量
     */
    @Column(name = "hcQuantity")
    @ApiModelProperty(value = "回厂方量", name = "hcquantity")
    private Float hcquantity;
    /**
     * 累计出厂方量
     */
    @Column(name = "ccsumQuantity")
    @ApiModelProperty(value = "累计出厂方量", name = "ccsumquantity")
    private Float ccsumquantity;
    /**
     * 拌车称重打印 0：否 1：是
     */
    @Column(name = "isPrint")
    @ApiModelProperty(value = "拌车称重打印 0：否 1：是", name = "isprint")
    private Integer isprint;
    /**
     * 出厂累计车数
     */
    @Column(name = "ccCarNumber")
    @ApiModelProperty(value = "出厂累计车数", name = "cccarnumber")
    private Integer cccarnumber;
    /**
     * 回厂剩余时间
     */
    @Column(name = "hcsyTime")
    @ApiModelProperty(value = "回厂剩余时间", name = "hcsytime")
    private Date hcsytime;
    /**
     * 签收剩余方量
     */
    @Column(name = "qrsyQuantity")
    @ApiModelProperty(value = "签收剩余方量", name = "qrsyquantity")
    private Float qrsyquantity;
    /**
     * 调度剩余方量
     */
    @Column(name = "ddsyQuantity")
    @ApiModelProperty(value = "调度剩余方量", name = "ddsyquantity")
    private Float ddsyquantity;
    /**
     * 调度剩余时间
     */
    @Column(name = "ddsyTime")
    @ApiModelProperty(value = "调度剩余时间", name = "ddsytime")
    private Date ddsytime;
    /**
     * 调度签收方量
     */
    @Column(name = "ddqrQuantity")
    @ApiModelProperty(value = "调度签收方量", name = "ddqrquantity")
    private Float ddqrquantity;
    /**
     * 调度签收时间
     */
    @Column(name = "ddqrTime")
    @ApiModelProperty(value = "调度签收时间", name = "ddqrtime")
    private Date ddqrtime;
    /**
     * 退料方量
     */
    @Column(name = "returnQuantity")
    @ApiModelProperty(value = "退料方量", name = "returnquantity")
    private Float returnquantity;
    /**
     * 退料时间
     */
    @Column(name = "returnTlTime")
    @ApiModelProperty(value = "退料时间", name = "returntltime")
    private Date returntltime;
    /**
     * 司机签收方量
     */
    @Column(name = "sjqrQuantity")
    @ApiModelProperty(value = "司机签收方量", name = "sjqrquantity")
    private Float sjqrquantity;
    /**
     * 司机签收时间
     */
    @Column(name = "sjqrTime")
    @ApiModelProperty(value = "司机签收时间", name = "sjqrtime")
    private Date sjqrtime;
    /**
     * 退料操作员
     */
    @Column(name = "returnTlczy")
    @ApiModelProperty(value = "退料操作员", name = "returntlczy")
    private String returntlczy;
    /**
     * 拌车称重打印次数
     */
    @Column(name = "weightPrintnum")
    @ApiModelProperty(value = "拌车称重打印次数", name = "weightprintnum")
    private Integer weightprintnum;

    @Transient
    @ApiModelProperty(value = "转料来源小票号")
    private String fromItemoderNo;

    @Transient
    @ApiModelProperty(value = "小票代表数量")
    private Float behalfNumber;

    @Transient
    @ApiModelProperty(value = "检测类型 1-快检 2-批检")
    private Integer checkType;

    /**
     * 0-作废 1-生产中(waiting) 2-运输中(finish) 3-已完成(complete)
     */
    @Transient
    @ApiModelProperty(value = "状态数组", name = "stateList")
    private List<Integer> stateList;

    @Transient
    @ApiModelProperty(value = "发货人", name = "fhy")
    private String fhy;

    @Transient
    @ApiModelProperty(value = "试验台账主键id")
    private Long experimentId;

    @Transient
    @ApiModelProperty(value = "品控小票扩展信息")
    private ItemorderExtend itemorderExtend;

    @Transient
    @ApiModelProperty(value = "任务编号")
    private String frwno;

    /**
     * 组装试验台账记录
     *
     * @param entity         小票信息
     * @param experimentType 试验类型
     * @return
     */
    public static Experiment payloadExperiment(Itemorder entity, Integer experimentType) {
        Experiment experiment = new Experiment();
        experiment.erpInit(true);
        experiment.setExperimentType(experimentType);
        experiment.setEntrustTime(new Date());
        experiment.setSampleLevel(entity.getTpz());
        experiment.setMaterialsName(entity.getTpz());
        experiment.setBehalfNumber(Double.valueOf(entity.getFhquantity()));
        experiment.setEntrustReasonCode(EnturstReasonEnum.CJ.getCode());
        experiment.setEntrustReasonName(EnturstReasonEnum.CJ.getName());

        /**获取对应小票扩展信息*/
        try {
            if (StringUtils.isNotBlank(entity.getExtrainfo())) {
                String entityExtrainfo = StringEscapeUtils.unescapeJava(entity.getExtrainfo());
                JSONObject jsonObject = JSON.parseObject(entityExtrainfo);
                experiment.setEntrustPersonName(jsonObject.getString("fhy"));
            }
        } catch (Exception e) {
            experiment.setEntrustPersonName("");
        }
        return experiment;
    }
}
